/**
 * Created by ${PRODUCT_NAME}.
 * User: skylerparr
 * Date: 12/11/10
 * Time: 11:29 PM
 * To change this template use File | Settings | File Templates.
 */
package com.thoughtorigin.injector {
import com.thoughtorigin.exception.UnsupportedOperationException;
import com.thoughtorigin.flux.Flux;
import com.thoughtorigin.reflect.MetaDataItem;
import com.thoughtorigin.meta.MetaType;

public class SimpleInjector implements Injector, MetaType {

    private static var _flux: Flux;
    private var _metadata: MetaDataItem;

    public function SimpleInjector(value: Flux = null) {
//        if(_flux) {
//            _flux = value;
//        }
    }

    public function createAndInject(clazz: Class, callback:Function): void {
        throw new UnsupportedOperationException("Method not implemented");
    }

    public function inject(object: Object, callback: Function): void {
        if(_metadata.metaArgs.length > 0) {
            object[_metadata.decoratedPropertyName] = _flux.getProperty(_metadata.metaArgs[0]);
        } else {
            try {
                object[_metadata.decoratedPropertyName] = _flux.getProperty(_metadata.decoratedPropertyName);
            } catch(e: Error) {
                trace(e.message);
            }
        }
        if(object.hasOwnProperty("flux")) {
            object["flux"] = _flux;
        }

        callback.call(this);
    }

    public function decorate(object: Object, metaData: MetaDataItem, callback: Function):void {
        _metadata = metaData;
        inject(object, callback);
    }

    public function get priority():int {
        return -20;
    }

    public static function set flux(value:Flux):void {
        _flux = value;
    }

    public function dispose(... args):void {
    }

    public function preprocess(clazz:Class, metaData: MetaDataItem):void {
        //not implemented as of now.
    }

    public function set metaData(value: MetaDataItem): void {
        _metadata = value;
    }

}
}
